package com.core.servicelayer;

import java.util.HashSet;
import java.util.Set;

import com.core.protocols.IHibernateParentEnumeratorDelegate;
import com.core.protocols.IKVObject;
import com.core.utils.HibernateParameter;
import com.model.ActionBarItem;

public class ActionBarServiceLayer
        extends HibernateServiceLayer {

	public Class <? extends IKVObject> entityClass () {

		return ActionBarItem.class;
	}



	public void enumerateParentObjectsWithDelegate ( IKVObject kvObject,
	                                                 IHibernateParentEnumeratorDelegate delegate ) {

	}



	public void performRequestWithUserContextAndUserId ( String userContextName,
	                                                     int userId ) {

		String script = null;
		String selectionCriteria = null;
		Set <HibernateParameter> params = new HashSet <HibernateParameter>();

		if ( userContextName != null ) {
			// TODO: need to be refactored

			String[] itemContextNames = userContextName.split(";");
			StringBuilder sb = new StringBuilder();
			Boolean needComa = false;
			for ( String name : itemContextNames ) {
				sb.append(String.format("%s'%s'", needComa ? "," : "", name));
				needComa = true;
			}

			selectionCriteria = "ActionBarItem";

			script = "";
			script += " join ActionBarItem.userContextNameVisibleIn UCN_Visible";
			script += " join ActionBarItem.userContextNameToExecute as UCN_ToExecute,";
			script += " com.model.user.User as User";
			script += " join User.userCategory.userRole.availableContexts as availableUCN";
			script += " where UCN_ToExecute IN (availableUCN)";
			script += " and UCN_Visible.name in " + String.format("(%s)", sb.toString());
			script += " and User.id=:ID";

			params.add(new HibernateParameter("ID", userId));
		}

		this.setSelectionCriteria(selectionCriteria);
		this.performRequestWithFilter(script, params);

	}
}
